﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections;

namespace ThemeLoader
{
    public class ThemeManager : IThemeManager
    {
        private IResourceProvider mergedDictionaryProvider;
        private Action recreateView;

        public ThemeManager(IResourceProvider mergedDictionaryProvider, Action recreateView)
        {
            this.mergedDictionaryProvider = mergedDictionaryProvider;
            this.recreateView = recreateView;
        }

        ResourceDictionary previousTheme = null;

        public void ApplyTheme(ResourceDictionary theme)
        {
            var mergedDictionary = mergedDictionaryProvider.GetResourceDictionary().MergedDictionaries;
            mergedDictionary.Clear();

            if (this.previousTheme != null)
            {
                mergedDictionary.Remove(this.previousTheme);
            }

            mergedDictionary.Add(theme);
            this.previousTheme = theme;

            this.recreateView();
        }
    }
}
